import { HttpClient, HttpParams, HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { CommentInfo } from './comment';


@Injectable()
export class CommentDataProvider {
  private readonly baseUrl: string = 'https://api.bmob.cn/1';
  private readonly httpHeaders : HttpHeaders =new HttpHeaders({
    'X-Bmob-Application-Id':'0f23034457b7a694277ce9d417af0538',
    'X-Bmob-REST-API-Key':'743577773d54228e84ae14b11573af5a',
    'Content-Type':'application/json'
    });


  constructor(public http: HttpClient) {
    
  }
  getCommentByParkId(objectId: string): Observable<any[]>{
    let whereStr = `{"parkId":{"__type":"Pointer","className":"parks","objectId":"${objectId}"}}`;
    let _params = new HttpParams()
    .set('where',whereStr)
    .set('include','parkId,userId')
    .set('order', '-updateAt');
    return this.http.get<any[]>(`${this.baseUrl}/classes/comments`,{headers:this.httpHeaders,params:_params}).map(val=> val["results"]);
  }
/**
 * 新增点评
 * @param commentInfo 
 */
addComment(commentInfo: CommentInfo): Observable<CommentInfo>{
  let _commentInfo = {
    content: commentInfo.content,
    userId: {
      __type: "Pointer",
      className: "_User",
      objectId:commentInfo.userId
    },
    parkId: {
      __type: "Pointer",
      className: "parks",
      objectId: commentInfo.parkId
    },
  }

    return this.http.post<CommentInfo>(`${this.baseUrl}/classes/comments`,_commentInfo, { headers: this.httpHeaders });
   }

}
